package com.tnsw.noxnox;

import com.tnsw.coreutils.AsyncManager;
import com.tnsw.noxnox.db.DBManager;
import com.tnsw.noxnox.dialog.FragmentAlertDialog;
import com.tnsw.noxnox.downloader.DLBase;
import com.tnsw.noxnox.downloader.DLInterface;
import com.tnsw.noxnox.downloader.DailyMotionDL;
import com.tnsw.noxnox.downloader.DownloaderService;
import com.tnsw.noxnox.downloader.GCMManagerService;
import com.tnsw.noxnox.downloader.GCMReceiverInterface;
import com.tnsw.noxnox.downloader.YoutubeDL;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class YoutubeFragment extends FragmentWithAdapter implements OnClickListener, DLInterface, Runnable, GCMReceiverInterface{

	private String tag = "YoutubeFragment";
	private DBManager dbm;
	private AsyncManager asyncManager;
	private GCMManagerService gcms;
	
	private Button btnLoad, btnStop, btnPause, btnDailyMotion, btnDownloadNow;
	private TextView txtUrl, lblStatus;
	private ProgressBar progressBar1;
	
	private DLBase dl;
	
	private ViewGroup root;
	
	public YoutubeFragment() {
		super();
		Log.d(tag, "Constructor()");
		this.dbm = DBManager.getInstance(context);
		this.asyncManager = AsyncManager.getInstance();
		this.gcms = GCMManagerService.getInstance();
		this.gcms.registerReceiver(this);
	}
	
	public YoutubeFragment(Context context, FragmentPagerAdapter pagerAdapter) {
		super(context, pagerAdapter);
		Log.d(tag, "Constrcutor(context)");
		this.context = context;
		this.dbm = DBManager.getInstance(context);
		this.asyncManager = AsyncManager.getInstance();
		this.gcms = GCMManagerService.getInstance();
		this.gcms.registerReceiver(this);
	}
	
	
	public static Fragment newInstance(Context context, FragmentPagerAdapter pagerAdapter) {
		Log.d("YoutubeFragment", "newInstance()");
		YoutubeFragment f = new YoutubeFragment(context, pagerAdapter);
		return(f);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		this.root = (ViewGroup) inflater.inflate(R.layout.activity_youtube_test, null);
		
		this.btnLoad = (Button) this.root.findViewById(R.id.btnLoad);
		this.btnLoad.setOnClickListener(this);
		
		this.btnStop = (Button) this.root.findViewById(R.id.btnStop);
		this.btnStop.setOnClickListener(this);
		this.btnStop.setVisibility(View.INVISIBLE);
		
		this.btnPause = (Button) this.root.findViewById(R.id.btnPause);
		this.btnPause.setOnClickListener(this);
		this.btnPause.setVisibility(View.INVISIBLE);
		
		this.btnDailyMotion = (Button) this.root.findViewById(R.id.btnDailyMotion);
		this.btnDailyMotion.setOnClickListener(this);
		
		this.btnDownloadNow = (Button) this.root.findViewById(R.id.btnDownloadNow);
		this.btnDownloadNow.setOnClickListener(this);
		
		this.txtUrl = (TextView) this.root.findViewById(R.id.txtUrl);
		
		this.progressBar1 = (ProgressBar) this.root.findViewById(R.id.progressBar1);
		this.lblStatus = (TextView) this.root.findViewById(R.id.lblStatus);
		
		return(root);
	}

	@Override
	public void onReceiveMessage(Intent intent) {
		Log.d(tag, "Yes, I got message");
		
		Bundle b = intent.getExtras();
		if (b.containsKey("value")) {
			String url = b.getString("value");
			
			this.txtUrl.setText(url);
		}
		
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProgressUI(DLBase sender) {
		this.progressBar1.setMax((int) sender.getMax());
		this.progressBar1.setProgress(sender.getCurrentPosition());
		
		this.lblStatus.setText(sender.getTextStatus());
		
	}

	@Override
	public void onProgressBG(DLBase sender) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onCompletedUI(DLBase sender, long result) {
		this.lblStatus.setText(sender.getTextStatus());
		if (result == 0) {
			this.btnPause.setVisibility(View.INVISIBLE);
			this.btnStop.setVisibility(View.INVISIBLE);
		}
		
	}

	@Override
	public void onStartedBG(DLBase sender) {
		Activity a = (Activity) this.context;
		
		a.runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				btnPause.setVisibility(View.VISIBLE);
				btnStop.setVisibility(View.VISIBLE);
				
			}
		});
		
	}

	@Override
	public void onClick(View v) {
		if (v == this.btnLoad) {
			//String urlx = "http://www.youtube.com/watch?v=9bZkp7q19f0";
			//urlx = "http://www.youtube.com/watch?annotation_id=annotation_57484&feature=iv&src_vid=mfxDgbPk_QA&v=q7EQbpcOPPM";
			//urlx = "http://www.youtube.com/watch?v=NKE-RXR_XIs&feature=youtube_gdata_player";
			//this.txtUrl.setText(urlx);
			String url = this.txtUrl.getText().toString();
			if (url.equals("")) {
				url = "http://www.youtube.com/watch?v=9bZkp7q19f0";
			}
			
			Log.d(tag, "downloading URL: " + url);
			
			this.dl = new YoutubeDL(this.context, url);
			this.dl.setDLInterface(this);
			this.dl.doit();
			
		} else if (v == this.btnDailyMotion) {
			//http://www.dailymotion.com/video/xh0va3_sex-is-zero_sexy#.UTZAy3yPhJE
			//http://www.dailymotion.com/en/relevance/search/natalie+korea/1#video=xg3szz
			String url = this.txtUrl.getText().toString();
			//this.txtUrl.setText("http://www.dailymotion.com/video/x4z9f2_nude-beach-004_sexy#.UTgHyHyPhJE");
			
			if (url.equals("")) {
				url = "http://www.dailymotion.com/video/xh0va3_sex-is-zero_sexy#.UTZAy3yPhJE";
			}
			
			this.dl = new DailyMotionDL(this.context, url);
			this.dl.setDLInterface(this);
			this.dl.doit();
			
		} else if (v == this.btnPause) {
			if (this.dl.getCurrentHttpBase() != null) {
				if (this.dl.isPaused() == true) {
					this.dl.resume();
					this.btnPause.setText("Pause");
				} else {
					this.dl.pause();
					this.btnPause.setText("Resume");
				}
			}
			
		} else if (v == this.btnDownloadNow) {
			DownloaderService ds = DownloaderService.getInstance(this.context);
			String url = this.txtUrl.getText().toString();
			if (url.equals("") == true) {
				url = "http://www.youtube.com/watch?v=5Bh49ch5mw8";
			}
			this.txtUrl.setText(url);
			ds.doDownload(this.txtUrl.getText().toString());
		}
		
	}

	@Override
	public void onError(DLBase sender, boolean isBackground, String message, Exception e) {
		FragmentAlertDialog dialog = new FragmentAlertDialog(this.context, this);
		dialog.setTitle("Error");
		dialog.setMessage(message);
		dialog.show(this.getFragmentManager(), "error1");
		
		//this.getFragmentManager().findFragmentByTag("error1");
	}
}
